package net.linku.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.linku.domain.LAChangeRecord;
import net.linku.enums.StatusEnum;
import net.linku.mapper.LAChangeRecordMapper;
import net.linku.utils.TenantContext;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * LAChangeRecordRepository 是用于处理 LAChangeRecord 实体的数据库操作的仓库类。
 *
 * @version 1.0
 * @date 2024/7/15
 */
@Slf4j
@Repository
public class LAChangeRecordRepository extends ServiceImpl<LAChangeRecordMapper, LAChangeRecord> {

    /**
     * 批量插入变更记录。
     *
     * @param changeRecordList 需要插入的变更记录列表
     * @return 如果插入成功返回 true，否则返回 false
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public boolean insertBatch(List<LAChangeRecord> changeRecordList) {
        Long tenantId = Long.valueOf(TenantContext.getTenantId());
        try {
            // 为每个记录设置 tenant_id
            changeRecordList.forEach(record -> record.setTenantId(tenantId));
            boolean result = saveBatch(changeRecordList);
            if (!result) {
                log.error("批量插入变更记录失败: {}", changeRecordList);
            }
            return result;
        } catch (DataAccessException e) {
            log.error("批量插入变更记录时发生数据库访问异常: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("批量插入变更记录时发生异常: {}", e.getMessage());
            throw e;
        }
    }

    /**
     * 根据条件查询变更记录，并分页返回结果。
     *
     * @param changeRecord 查询条件
     * @param pageSize     每页记录数
     * @param pageNum      当前页码
     * @return 分页结果
     */
    public Page<LAChangeRecord> queryListByCondition(LAChangeRecord changeRecord, Integer pageSize, Integer pageNum) {
        Page<LAChangeRecord> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LAChangeRecord> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (changeRecord.getBusinessType() != null) {
            queryWrapper.eq(LAChangeRecord::getBusinessType, changeRecord.getBusinessType());
        }
        if (changeRecord.getBusinessId() != null) {
            queryWrapper.eq(LAChangeRecord::getBusinessId, changeRecord.getBusinessId());
        }
        // 添加删除标志
        queryWrapper.eq(LAChangeRecord::getDeletedFlag, StatusEnum.NORMAL.getDeletedFlag());
        // 添加排序条件
        queryWrapper.orderByDesc(LAChangeRecord::getCreationDate);

        try {
            return page(page, queryWrapper);
        } catch (DataAccessException e) {
            log.error("查询变更记录时发生数据库访问异常: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("查询变更记录时发生异常: {}", e.getMessage());
            throw e;
        }
    }
}
